<?php
namespace Partner\Controller;

use Utuo\BackgroundController;

class AdPositionController extends BackgroundController{
	
	protected function _initialize() {
		$this->setControllerTitle("合作商广告位管理 ");
		$this->setActionTitle(array(
				'index' => '广告位列表',
				'modify' => '设置广告',
		));
	}
	
	// 合作商广告位列表
	public function indexAction() {
		$this->getAdPositionList(0);
	}
	
	
	// 修改广告页
	public function modifyAction() {
		$id = I("id", 0, "intval");
		$adPosition = $this->getOneById("PartnerAdPosition",$id);
		$this->checkAdmin($adPosition['partner']);
		$partnerAds = array();
		$partnerAds[] = $partnerAd = D("PartnerAd")->find($adPosition['news']); // 广告的图文信息
		$adPosition['adTypesOptions'] = $this->getOption(D("PartnerAdPosition")->adTypes, $adPosition['ad_type'], '==请选择类型==');
		$this->assign("partnerAdJSON", empty($partnerAd)?"{}":json_encode(addID($partnerAds,'id','id-')));
		$this->assign("adPosition", $adPosition);
		$this->display();
	}
	
	// 修改广告页
	public function modifyPostAction() {
		$d = D('PartnerAdPosition');
		if ($d->create()) {
			unset($d->partner);
			$this->checkAdmin(null, $d,"partner");
			if (false !== $d->save()) {
				$this->ajaxSuccess('广告设置成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 移除菜单
	public function setStatusPostAction() {
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$partnerAdPosition = M("PartnerAdPosition");
		$this->checkAdmin(null, $partnerAdPosition, "partner");
		if ($partnerAdPosition->where("id={$id}")->setField("status", 1 - $status)) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($partnerAdPosition->getError());
		}
	}
	
	// 回收站
	public function recycleAction() {
		$this->getAdPositionList(1);
	}
	
	// 获得合作商广告位列表
	public function getAdPositionList($is_deleted){
		$positionTypes = M("PartnerAdPositionType")->getField("id,name");
		foreach ($positionTypes as $k => $v){
			$condition = array(
						'partner'=>$this->aid,
						'position_type'=>$k);
			if(!M("PartnerAdPosition")->where($condition)->limit(1)->count()){
					if(!M("PartnerAdPosition")->add(array(
						'position_type' => $k,
						'partner'=>$this->aid,
					))){
						$this->error(M("PartnerAdPosition")->getDbError());
					}
					
			}
		}
		$adPositionLists = M("PartnerAdPosition")->where(array('partner'=>$this->aid,))->select();
		foreach ($adPositionLists as &$v) {
			$v['position_type_name'] = $positionTypes[$v['position_type']];
		}
		$this->assign("adPositionLists",$adPositionLists);
		$this->display();
	}
	
	
	// 获得广告图文列表
	public function getAdListPostAction() {
		$p = I('get.p',1,'intval');
		$pageSize = 10;
		$d = M("PartnerAd")->field("id,name,pic")->where(array('partner' => $this->aid, 'is_deleted' => 0));
		$adLists = $d->limit(($p-1)*$pageSize.','.$pageSize)->order("id desc")->select();
		if ($adLists || !$d->getError()) {
			$this->ajaxSuccess('', $adLists);
		} else {
			$this->ajaxError("获取广告图文列表失败");
		}
	}
	
	public function TestAction(){
		D("PartnerAd");
	}
	
}	